document.getElementById("entries").addEventListener("DOMNodeInserted",findVideo, false);
var allaTags = new Array();
var vlink;
var cont;
function findVideo(e) {
  //console.log(e.target)
  if(e.target.parentNode == document.getElementById("entries") || e.target.className == "entry-container"){
    allaTags = e.target.getElementsByTagName("a");
    for (i=0; i<allaTags.length; i++) {
      cont = allaTags[i].parentNode;
      if ((cont.className == "view-enclosure-parent") && (allaTags[i].href.search(/mp4$/) != -1 || allaTags[i].href.search(/m4v$/) != -1 || allaTags[i].href.search(/mp4v$/) != -1)){
        vlink = allaTags[i];
        //console.log(allaTags);
        cont = vlink.parentNode.parentNode.parentNode;
        if(cont.firstChild.className != "audio-player-container"){
          createvideo(cont,vlink.href);
        }
      }
    }
  }else if(e.target.parentNode == document.getElementById("current-entry") && e.target.className == "entry-container"){

  }
}


var fullscreen = false;
var vdrag = false;
var sdrag = false;

function createvideo(cont,link){
    var playercont = document.createElement('div');
        playercont.setAttribute('class','audio-player-container');
    var videoplayer = document.createElement('div');
        videoplayer.setAttribute('id','videoplayer');
    var video = document.createElement('video');
        video.setAttribute('id','video');
  	    video.setAttribute('width', '480');
  	    video.setAttribute('height', '270');
  	    video.setAttribute('src', link);
    var controles = document.createElement('div');
        controles.setAttribute('id','controles');
    var controlestable = document.createElement('div');
        controlestable.setAttribute('id','controlestable');
    var playb = document.createElement('div');
        playb.setAttribute('id','play');
        playb.setAttribute('class','button');
    var videoslider = document.createElement('div');
        videoslider.setAttribute('id','videoslider');
    var vsliderback = document.createElement('div');
        vsliderback.setAttribute('id','vsliderback');
        vsliderback.setAttribute('class','sliderback');
    var vslidercont = document.createElement('div');
        vslidercont.setAttribute('id','vslidercont');
        vslidercont.setAttribute('class','slidercont');
    var loaded = document.createElement('div');
        loaded.setAttribute('id','loaded');
    var viewed = document.createElement('div');
        viewed.setAttribute('id','viewed');
        viewed.setAttribute('class','sliderac');
    var timecont = document.createElement('div');
        timecont.setAttribute('id','timecont');
    var time = document.createElement('span');
        time.setAttribute('id','time');
        time.innerHTML = "0:00";
    var duration = document.createElement('span');
        duration.setAttribute('id','duration');
        duration.innerHTML = " / 0:00";
    var full = document.createElement('div');
        full.setAttribute('id','full');
        full.setAttribute('class','button');
    var nwindow = document.createElement('div');
        nwindow.setAttribute('id','nwindow');
        nwindow.setAttribute('class','button');
    var soundslider = document.createElement('div');
        soundslider.setAttribute('id','soundslider');
    var sslidercont = document.createElement('div');
        sslidercont.setAttribute('id','sslidercont');
        sslidercont.setAttribute('class','slidercont');
    var ssliderback = document.createElement('div');
        ssliderback.setAttribute('id','ssliderback');
        ssliderback.setAttribute('class','sliderback');
    var sound = document.createElement('div');
        sound.setAttribute('id','sound');
        sound.setAttribute('class','sliderac');

    full.onclick = vfullscreen(playercont,videoplayer,video,controles,playb,full);
    playb.onclick = playvideo(video);
    nwindow.onclick = newwindow();
    video.addEventListener("play", function() {playb.setAttribute('id','pause');}, true);
    video.addEventListener("pause", function() {playb.setAttribute('id','play');}, true);
		video.addEventListener('durationchange',function(e) {vsetduration(duration,timecont,e)}, true);
		video.addEventListener('timeupdate',function(e) {vtimeupdate(time,viewed,e)}, true);
		video.addEventListener('volumechange',function(e) {volumeupdate(sound,e)}, true);
		video.addEventListener('progress',function(e) {vprogressupdate(loaded,e)}, true);

    videoslider.onmousedown = acvslider(video,controles,videoslider,viewed,loaded);
    soundslider.onmousedown = acsslider(video,controles,soundslider,sound);

    videoplayer.appendChild(video);
    controlestable.appendChild(playb);
    vslidercont.appendChild(vsliderback);
    vslidercont.appendChild(loaded);
    vslidercont.appendChild(viewed);
    videoslider.appendChild(vslidercont);
    controlestable.appendChild(videoslider);
    timecont.appendChild(time);
    timecont.appendChild(duration);
    controlestable.appendChild(timecont);
    sslidercont.appendChild(ssliderback);
    sslidercont.appendChild(sound);
    soundslider.appendChild(sslidercont);
    controlestable.appendChild(soundslider);
    controlestable.appendChild(nwindow);
    controlestable.appendChild(full);
    controles.appendChild(controlestable);
    videoplayer.appendChild(controles);
    playercont.appendChild(videoplayer);
    cont.insertBefore(playercont,cont.firstChild);
}

function playvideo(video){
  return function() {
    if (video.paused){
      video.play();
    }else{
      video.pause();
    }
    return false;
  }
}

function vsetduration(duration,timecont,e){
  	var s=e.target.duration;
  	var h=Math.floor(s/3600);
  	s=s%3600;
  	var m=Math.floor(s/60);
  	s=Math.floor(s%60);
  	var time;
  	var timel;
  	if (s.toString().length < 2) s="0"+s;
    if(h==0){
      time = " / " + m+":"+s;
      timel = 15 + 2 * (7*(m.toString().length + s.toString().length));
    }else{
    	if (m.toString().length < 2) m="0"+m;
      time = " / " + h+":"+m+":"+s;
      timel = 21 + 2 * (7*(h.toString().length + m.toString().length + s.toString().length));
    }
    duration.innerHTML = time;
    timecont.style.width = timel + 'px';
}

function vtimeupdate(time,viewed,e){
  	/* split currentTime (seconds) into separate hour/minute/second strings */
  	var s=e.target.currentTime;
  	var d=e.target.duration;
  	var p= 100-(100/d*s);
  	viewed.style.right = p + "%";

  	var h=Math.floor(s/3600);
  	s=s%3600;
  	var m=Math.floor(s/60);
  	s=Math.floor(s%60);
  	/* pad the minute and second strings to two digits */
  	if (s.toString().length < 2) s="0"+s;
    if(h==0){time.innerHTML = m+":"+s;}
  	else{
    	if (m.toString().length < 2) m="0"+m;
      time.innerHTML = h+":"+m+":"+s;
    }
}

function volumeupdate(sound,e){
  	var v= 100-(100*e.target.volume);
  	sound.style.right = v + "%";
}

function vprogressupdate(bar,e){
   var l = e.target.loaded;
   var t = e.target.total;
   var pos = 100-(100/t*l);
   pos = 0;
   bar.style.right = pos + "%";
}

var accontroles;
var acvideoslider;
var acviewed;
var acloaded;
var acsoundslider;
var acsound;

function acvslider(video,videoplayer,videosliderviewed,loaded){
  return function() {
    var ev = window.event;
    vdrag = true;
    acvideo = video;
    accontroles = controles;
    acvideoslider = videoslider;
    acviewed = viewed;
    acloaded = loaded;
    dragslider(ev,acvideo,acvideoslider,acviewed,accontroles,"video");
    return false;
  }
}

function acsslider(video,controles,soundslider,sound){
  return function() {
    var ev = window.event;
    sdrag = true;
    acvideo = video;
    accontroles = controles;
    acsoundslider = soundslider;
    acsound = sound;
    dragslider(ev,acvideo,acsoundslider,acssound,accontroles,"sound");
    return false;
  }
}

window.onmousemove = function(e){
    var ev = window.event?window.event:e;
    if(sdrag){
      dragslider(ev,acvideo,acsoundslider,acsound,accontroles,"sound");
    }
    if(vdrag){
      dragslider(ev,acvideo,acvideoslider,acviewed,accontroles,"video");
    }
}

function dragslider(ev,video,acslidercont,acbar,accont,type){
    var pos = 100/(acslidercont.offsetWidth - 12) * (ev.clientX - (acslidercont.offsetLeft + 6) - accontroles.offsetLeft);
    if(pos<=0){pos=0;}
    if(pos>=100){pos=100;}
    //acbar.style.width = pos + "%";
    if(type=="video"){
      var seek = pos/100*video.duration;
      video.currentTime = seek;
    }
    if(type=="sound"){
      var seek = pos/100;
      video.volume = seek;
    }
}

window.onmouseup = function(){
  vdrag = false;
  sdrag = false;
}

function newwindow(){
  return function() {
    videowindow = window.open( newwindow.htm, "videowindow", "width=300,height=200");
    videowindow.focus();
    return false;
  }
}

function vfullscreen(playercont,videoplayer,video,controles,playb,full){
  return function() {
    if(!fullscreen){
      var playerh = playercont.offsettHeight;
      playercont.style.height = playerh + 'px';
      videoplayer.setAttribute('id','videoplayerfull');
      controles.setAttribute('id','controlesfull');
      full.setAttribute('id','smal');
      document.getElementById("entries").style.zIndex = "200 !important";
      video.setAttribute('height','100%');
      video.setAttribute('width','100%');
      fullscreen = true;
    }else{
      playercont.style.height = '';
      videoplayer.setAttribute('id','videoplayer');
      controles.setAttribute('id','controles');
      full.setAttribute('id','full');
      document.getElementById("entries").style.zIndex = '';
      video.setAttribute('height','270');
      video.setAttribute('width','480');
      fullscreen = false;
    }
    return false;
  }
}